package com.awei.study.design.structure.composite;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author liweilong
 * @Date 2024/8/8 16:56
 * 组合模式
 */
public class Composite {
    public abstract class MenuComponent {
        protected String name;
        protected int level;

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public int getLevel() {
            return level;
        }

        public void setLevel(int level) {
            this.level = level;
        }

        public void add(MenuComponent component) {
            throw new UnsupportedOperationException();
        }

        public void remove(MenuComponent component) {
            throw new UnsupportedOperationException();
        }

        public MenuComponent getChild(int i) {
            throw new UnsupportedOperationException();
        }

        public void print() {
            throw new UnsupportedOperationException();
        }
    }

    public class Menu extends MenuComponent {

        private List<MenuComponent> list = new ArrayList<>();

        public Menu(String name ,int level) {
            this.name = name;
            this.level = level;
        }

        @Override
        public void add(MenuComponent component) {
            list.add(component);
        }

        @Override
        public void remove(MenuComponent component) {
            list.remove(component);
        }

        @Override
        public MenuComponent getChild(int i) {
            return list.get(i);
        }

        @Override
        public void print() {
            for (int i = 0; i < this.level; i++) {
                System.out.println("--");
            }
            System.out.println(getName());
            for (MenuComponent menuComponent : list) {
                menuComponent.print();
            }
        }
    }

    public class MenuItem extends MenuComponent {
        public MenuItem(String name ,int level) {
            this.level = level;
            this.name = name;
        }

        @Override
        public void print() {
            for (int i = 0; i < this.level; i++) {
                System.out.println("--");
            }
            System.out.println(getName());
        }
    }

    public static void main(String[] args) {

    }
}
